#ifndef TIMESYSTEM_H
#define TIMESYSTEM_H

#include <SDL/SDL.h>

//This class does the time would be exactly the
//same in all computers.
class TimeSystem
{
	public:
    /** Everything is initialized to zero */
		TimeSystem();

    /** Remove the thread */
		~TimeSystem();
		
    /**
      * Initialize all the variables with @see Setting.
      * Throw the thread where @see masterTick is incremented
      *
      * @returns true if there wasn't any errors throwing the thread.
      */
    bool load();

    /** 
      * Check if the game has to do a logic cicle.
      * @returns true if the game has.
      */
		bool isLogicTick();
    /**
      * Check if the game has to update the screen.
      * @returns true if the game has.
      */
		bool isGraphicTick();

		/**
      * @returns how many logic cicles the game does in a second.
      */
		unsigned ticksPerSecond()const;

		/**
      * @returns how many miliseconds there are among logic cicles.
      */
		float getTimeTick()const;

	protected:
		/**
      * Backcall function executed in other thread 
      * and increments the masterTick.
      *
      * @param interval is the time it waits to increment it. In this case
      * it is @see m_tickTime.
      * @param parameter it doesn't use.
      */
		Uint32 static tickCounter(Uint32 interval,void *parameter);

		/** This variable grows one unit each @see m_tickTime by @see tickCounter*/
		static volatile unsigned long masterTick; 

		unsigned long time;
		/** This variable is the time there is among logic ticks */
		unsigned m_tickTime;
		/** How many consecutives frameskip happened*/
		unsigned m_actualFrameskip;
    /** How many consecutives frameskip are allowed */
    unsigned m_maxFrameskip;
		/** How many frameskip had happen in the actual second*/
		unsigned m_totalFrameskip;

    /** How many frames per second are allowed */
		unsigned int m_maxFPS;
		/** The actual graphic ticks in this second */
		unsigned int m_actualGraphicTick;
		/** The actual logic ticks*/
		unsigned long m_actualLogicTick;
    /** It's use to know when a second happens */
    unsigned long m_oldTick;

    /** Save the thead ID to remove it later */
		SDL_TimerID timerID;
};

#endif
